package zsc.liangyi.blog.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import zsc.liangyi.blog.dao.ArticleDao;
import zsc.liangyi.blog.dao.CommentMapper;
import zsc.liangyi.blog.dao.StatisticMapper;
import zsc.liangyi.blog.model.domain.Comment;
import zsc.liangyi.blog.model.domain.Statistic;
import zsc.liangyi.blog.service.CommentService;

import java.util.List;

@Service
@Transactional
public class CommentServiceImpl implements CommentService {
    @Autowired
    private StatisticMapper statisticMapper;
    @Autowired
    private ArticleDao articleMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Override
    public PageInfo<Comment> getComments(Integer aid, int page, int count) {
        PageHelper.startPage(page,count);
        List<Comment> commentList=commentMapper.selectCommentWithPage(aid);
        PageInfo<Comment> commentPageInfo=new PageInfo<>(commentList);
        return commentPageInfo;
    }

    @Override
    public void pushComment(Comment comment) {
        commentMapper.pushComment(comment);
        //更新该文章评论量
        //获取统计信息
        Statistic statistic=statisticMapper.selectStatisticWithArticleId(comment.getArticleId());
        //设置，更新统计信息
        statistic.setCommentsNum(statistic.getCommentsNum()+1);
        statisticMapper.updateArticleCommentsWithId(statistic);
    }
}
